﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace JasRibbonApplicatie.Views
{
    /// <summary>
    /// Interaction logic for ToebehorenToevoegen.xaml
    /// </summary>
    public partial class ToebehorenToevoegen : Page
    {
        jasEntities entities = new jasEntities();
        public ToebehorenToevoegen()
        {
            InitializeComponent();
        }

        private void toevoegButton_Click(object sender, RoutedEventArgs e)
        {
            toebehorentypes toe = new toebehorentypes();
            if (naamTextBox.Text != "" && afbeeldingPathTextBox.Text != "")
            {

                //afbeelding uploaden => put this in threaded task

                string localFilename = afbeeldingPathTextBox.Text;
                string remoteFilename = (naamTextBox.Text + " " + DateTime.Now.ToString("yy-MM-ddTHH-mm-ss")).Replace(' ', '_') + (new System.IO.FileInfo(localFilename)).Extension;

                #region ftp config
                OpenFTP.FTP f = new OpenFTP.FTP();
                f.Connect(Properties.Settings.Default.FTP_HOSTNAME, Properties.Settings.Default.FTP_USERNAME, Properties.Settings.Default.FTP_PASSWORD);
                f.ChangeDirectory(Properties.Settings.Default.FTP_BASEPATH + "img/toebehoren");
                #endregion
                f.Files.Upload(remoteFilename, localFilename);
                uploadProgressBar.Visibility = Visibility.Visible;
                vooruitgangLabel.Visibility = Visibility.Visible;
                while (!f.Files.UploadComplete)
                {
                    //Totaal aantal Bytes: " +  f.Files.TotalBytes.ToString()
                    vooruitgangLabel.Content = "Uploading: Percent voltooid: " + f.Files.PercentComplete.ToString() + "%";
                    uploadProgressBar.Value = f.Files.PercentComplete;
                }
                vooruitgangLabel.Content = "Upload Voltooid: Percent voltooid: 100%";
                uploadProgressBar.Value = 100;
                f.Disconnect();
                f = null;


                //wegschrijven naar db

                toe.naam = naamTextBox.Text;
                toe.afbeelding = "img/toebehoren/" + remoteFilename;
                entities.AddTotoebehorentypes(toe);
                entities.SaveChanges();
                MessageBox.Show("Database is bijgewerkt", "update", MessageBoxButton.OK);
                naamTextBox.Clear();
            }
            else
            {
                MessageBox.Show("Gelieve alle velden in te vullen", "opmerking", MessageBoxButton.OK);
            }
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Kies een afbeelding";
            ofd.InitialDirectory = @"C:\";
            ofd.Filter = "Afbeeldingen (*.jpg, *.png)|*.jpg;*.png;";
            ofd.FilterIndex = 2;
            ofd.RestoreDirectory = true;
            Nullable<bool> result = ofd.ShowDialog();
            if (result == true)
            {
                afbeeldingPathTextBox.Text = ofd.FileName;
            }
        }
    }
}
